﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_Channel
    /// </summary>
    public class DBLoader_Channel : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DBLoader_Channel(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = db.CreateConnection();
            if (!LoadChannelResource(conn))
                return false;

            if (!LoadChannelUserResource(conn))
                return false;
            return true;
        }

        private bool LoadChannelResource(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from ChannelResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int id = dr.GetInt32(0);
                    int left = dr.GetInt32(1);
                    int top = dr.GetInt32(2);
                    int right = dr.GetInt32(3);
                    int bottom = dr.GetInt32(4);
                    int channel_type = dr.GetInt32(5);
                    X2D.Boxf box = new X2D.Boxf();
                    box.begin = new X2D.Pointf(left, top);
                    box.end = new X2D.Pointf(right, bottom);

                    ChannelManager.RegisterChannelInfo(id, box, channel_type);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load ChannelResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Channel.LoadChannelResource Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} Channels loaded.", count);
            return true;
        }

        private bool LoadChannelUserResource(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from ChannelUserLimitResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int id = dr.GetInt32(0);
                    int proper_user = dr.GetInt32(1);
                    int max_channel_num = dr.GetInt32(2);

                    ChannelManager.RegisterUserLimitChannelInfo(id, proper_user, max_channel_num);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load ChannelUserLimitResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Channel.LoadChannelUserResource Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} ChannelUsers loaded.", count);
            return true;

        }


    }

}
